package com.pds.web.category.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pds.web.category.entity.PdsCategoryStandard;
import com.pds.web.category.mapper.PdsCategoryStandardMapper;
import com.pds.web.category.service.PdsCategoryStandardService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 检测项目标准表
 *
 * @author srf sunlightcs@gmail.com
 * @since 1.0.0 2024-02-29
 */
@Service("pdsCategoryStandardService")
public class PdsCategoryStandardServiceImpl extends ServiceImpl<PdsCategoryStandardMapper, PdsCategoryStandard> implements PdsCategoryStandardService {
    @Resource
    private PdsCategoryStandardMapper pdsCategoryStandardMapper;

    @Override
    public List<PdsCategoryStandard> selectList(PdsCategoryStandard pdsCategoryStandard) {
        QueryWrapper<PdsCategoryStandard> wrapper = new QueryWrapper<>();
        Long categoryId = pdsCategoryStandard.getCategoryId();
        String name = pdsCategoryStandard.getName();
        String status = pdsCategoryStandard.getStatus();
        if(!Objects.isNull(categoryId)){
            wrapper.eq("category_id",categoryId);
        }
        if(!Objects.isNull(name) && !name.isEmpty()){
            wrapper.like("name",name);
        }
        if(!Objects.isNull(status) && !status.isEmpty()){
            wrapper.eq("status",status);
        }
        return pdsCategoryStandardMapper.selectList(wrapper);
    }

    @Override
    public PdsCategoryStandard queryById(Long id) {
        return pdsCategoryStandardMapper.selectById(id);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public PdsCategoryStandard insert(PdsCategoryStandard pdsCategoryStandard) {
        pdsCategoryStandard.setStatus("1");
        pdsCategoryStandardMapper.insert(pdsCategoryStandard);
        return pdsCategoryStandard;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public Boolean update(PdsCategoryStandard pdsCategoryStandard) {
        return pdsCategoryStandardMapper.updateById(pdsCategoryStandard) > 0;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public Boolean deleteById(Long id) {
        return pdsCategoryStandardMapper.deleteById(id) > 0;
    }
}